package cn.zhou.jin.controller;

import cn.zhou.basic.exception.BusinessException;
import cn.zhou.basic.util.AjaxResult;
import cn.zhou.basic.util.ExcelUtil;
import cn.zhou.basic.util.PageList;
import cn.zhou.jin.domain.Shop;
import cn.zhou.jin.domain.ShopAuditLog;
import cn.zhou.jin.query.ShopQuery;
import cn.zhou.jin.service.IShopService;
import cn.zhou.jin.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;

    /**
     * 报表 数量 柱状图
     * @return
     */
    @GetMapping("/echarts")
    public Map<String,Object> echarts(){
        List<ShopVo> voList = shopService.queryEcharts();
        List<String> states = voList.stream().map(ShopVo::getState).collect(Collectors.toList());
        List<Integer> nums = voList.stream().map(ShopVo::getNum).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("x",states);
        map.put("y",nums);
        return map;
    }




    /**
     * 跳转接口   商家入驻注册
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        System.out.println(shop);
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 审核通过后端代码接口
     *   this.$http.post("/shop/audit/pass",this.shopAuditLog)
     */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.pass(log);
            return new AjaxResult();
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
     * 驳回接口
     *
     */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return new AjaxResult();
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /*
    * 导出报表
    *
    **/

    @GetMapping("/export/excel")
    public void export(HttpServletResponse response){
        List<Shop> shops = shopService.loadAll();
        ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"店铺信息.xls",response);
    }
    /**
     * 导入报表
     */
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        //批量新增
        shopService.adds(shops);
        System.out.println("导入的数据条数："+shops.size());

    }


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryPage(query);
    }
}
